package com.youge.springcloud.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.youge.springcloud.consumer.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: hcf
 * @qq: 46914685
 * @email: 46914685@qq.com
 * @date: 2020-10-20 23:40
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello")
    public String hello() {
        //调用SpringCloud服务者提供的服务
        //直接远程调用服务者提供的方式,不经过注册中心
//        return  restTemplate.getForEntity("http://localhost:8080/provider/hello",String.class).getBody();
        //利用服务提供者名称就可调用
        ResponseEntity<String> entity = restTemplate.getForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/hello", String.class);
        int statusCodeValue = entity.getStatusCodeValue();
        HttpStatus statusCode = entity.getStatusCode();
        HttpHeaders headers = entity.getHeaders();
        final String body = entity.getBody();

        System.out.println(statusCodeValue);
        System.out.println(statusCode);
        System.out.println(headers);
        System.out.println(body);


        return body;
    }

    @GetMapping("/user")
    public User user() {
        //调用SpringCloud服务者提供的服务
        //直接远程调用服务者提供的方式,不经过注册中心
//        return  restTemplate.getForEntity("http://localhost:8080/provider/hello",String.class).getBody();
        //利用服务提供者名称就可调用
        ResponseEntity<User> entity = restTemplate.getForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/user", User.class);
        int statusCodeValue = entity.getStatusCodeValue();
        HttpStatus statusCode = entity.getStatusCode();
        HttpHeaders headers = entity.getHeaders();
        User user = entity.getBody();

        System.out.println(statusCodeValue);
        System.out.println(statusCode);
        System.out.println(headers);
        System.out.println(user.getId() + " " + user.getName() + " " + user.getPhone());
        return user;
    }

    @GetMapping("/getUser")
    public User getUser() {
        //调用SpringCloud服务者提供的服务
        //直接远程调用服务者提供的方式,不经过注册中心
//        return  restTemplate.getForEntity("http://localhost:8080/provider/hello",String.class).getBody();
        //利用服务提供者名称就可调用
//        String[] strArray={"105","张无忌","1368000000"};
//
        Map<String, Object> paraMap = new ConcurrentHashMap<>();
        paraMap.put("id", 1024);
        paraMap.put("name", "张翠山");
        paraMap.put("phone", "139000000");

//        ResponseEntity<User> entity = restTemplate.getForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/getUser?id={0}&name={1}&phone={2}", User.class,strArray);
        User user = restTemplate.getForObject("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/getUser?id={id}&name={name}&phone={phone}", User.class, paraMap);
//        int statusCodeValue = entity.getStatusCodeValue();
//        HttpStatus statusCode = entity.getStatusCode();
//        HttpHeaders headers = entity.getHeaders();
//        User user = entity.getBody();
//
//        System.out.println(statusCodeValue);
//        System.out.println(statusCode);
//        System.out.println(headers);
        System.out.println(user.getId() + " " + user.getName() + " " + user.getPhone());
        return user;
    }


    /**
     * 测试  restTemplate.postForEntity 请求
     *
     * @return
     */
    @GetMapping("/addUser")
    public User addUser() {
        //调用SpringCloud服务者提供的服务
        //直接远程调用服务者提供的方式,不经过注册中心
//        return  restTemplate.getForEntity("http://localhost:8080/provider/hello",String.class).getBody();
        //利用服务提供者名称就可调用
//        String[] strArray={"105","张无忌","1368000000"};
//
        Map<String, Object> paraMap = new ConcurrentHashMap<>();
        paraMap.put("id", 1024);
        paraMap.put("name", "张翠山");
        paraMap.put("phone", "139000000");

        MultiValueMap<String, Object> dataMap = new LinkedMultiValueMap<>();
        dataMap.add("id", 1024);
        dataMap.add("name", "张翠山");
        dataMap.add("phone", "139000000");


        ResponseEntity<User> entity = restTemplate.postForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/addUser", dataMap, User.class);
        User user1 = restTemplate.postForObject("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/addUser", dataMap, User.class);
//        int statusCodeValue = entity.getStatusCodeValue();
//        HttpStatus statusCode = entity.getStatusCode();
//        HttpHeaders headers = entity.getHeaders();
        User user = entity.getBody();
//
//        System.out.println(statusCodeValue);
//        System.out.println(statusCode);
//        System.out.println(headers);
//        System.out.println(user.getId()+" "+user.getName()+" "+user.getPhone());

        System.out.println(user1.getId() + " " + user1.getName() + " " + user1.getPhone());
        return user;
    }

    @GetMapping("/updateUser")
    public String updateUser() {
        //调用SpringCloud服务者提供的服务
        //直接远程调用服务者提供的方式,不经过注册中心
//        return  restTemplate.getForEntity("http://localhost:8080/provider/hello",String.class).getBody();
        //利用服务提供者名称就可调用
//        String[] strArray={"105","张无忌","1368000000"};
//


        MultiValueMap<String, Object> dataMap = new LinkedMultiValueMap<>();
        dataMap.add("id", 1024);
        dataMap.add("name", "张翠山");
        dataMap.add("phone", "139000000");


        restTemplate.put("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/updateUser", dataMap);

        //
        return "success";
    }

    @GetMapping("/deleteUser")
    public String deleteUser() {
        //调用SpringCloud服务者提供的服务
        //直接远程调用服务者提供的方式,不经过注册中心
//        return  restTemplate.getForEntity("http://localhost:8080/provider/hello",String.class).getBody();
        //利用服务提供者名称就可调用
        String[] strArray = {"105", "张无忌", "1368000000"};

        MultiValueMap<String, Object> dataMap = new LinkedMultiValueMap<>();
        dataMap.add("id", 1024);
        dataMap.add("name", "张翠山");
        dataMap.add("phone", "139000000");

        Map<String, Object> paraMap = new ConcurrentHashMap<>();
        paraMap.put("id", 1024);
        paraMap.put("name", "张翠山");
        paraMap.put("phone", "139000000");


        restTemplate.delete("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/deleteUser?id={0}&name={1}&phone={3}", strArray);
        restTemplate.delete("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/deleteUser?id={id}&name={name}&phone={phone}", paraMap);

        //
        return "success";
    }

    /**
     * 测试熔断器
     *
     * @return
     */
    @GetMapping("/hystrix")
    @HystrixCommand(fallbackMethod = "error", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500") //设置超时时间
    },ignoreExceptions = Exception.class //忽略异常,这样不会执行错误回调设置的方法;但超时一样会触发熔断
    ) //熔断器,失败了会回调error方法
    public String hystrix() {
        return restTemplate.getForEntity("http://SPRINGCLOUD-SERVICE-PROVIDER/provider/hello", String.class).getBody();
    }

    private String error(Throwable throwable) {
//        访问远程服务失败,该如何处理?这些处理逻辑就可以写在该方法中
        System.out.println("异常:" + throwable.getMessage());
        return "error";
    }


}
